package com.wc.alorithm_luogu._单调队列.理想的正方形;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/29 21:22
 * @description https://www.luogu.com.cn/problem/P2216
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static int[][] a = new int[N][N];
    // 单调大栈
    static int[][] q1 = new int[N][N];
    static int[] hh1 = new int[N];
    static int[] tt1 = new int[N];
    // 单调小栈
    static int[][] q2 = new int[N][N];
    static int[] hh2 = new int[N];
    static int[] tt2 = new int[N];
    static int[] qq1 = new int[N];
    static int[] qq2 = new int[N];
    static int h1, t1, h2, t2;
    static int n, m, x;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        x = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= m; i++) {
            tt1[i] = tt2[i] = -1;
        }
        // 给 m 列 1 ~ x - 1行加入进去
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j < x; j++) {
                while (hh1[i] <= tt1[i] && a[j][i] >= a[q1[i][tt1[i]]][i]) tt1[i]--;
                q1[i][++tt1[i]] = j;
                while (hh2[i] <= tt2[i] && a[j][i] <= a[q2[i][tt2[i]]][i]) tt2[i]--;
                q2[i][++tt2[i]] = j;
            }
        }
        int res = (int) 2e9 - 10;
        for (int i = x; i <= n; i++) {
            // 将第 i 行加入
            for (int j = 1; j <= m; j++) {
                while (hh1[j] <= tt1[j] && a[i][j] >= a[q1[j][tt1[j]]][j]) tt1[j]--;
                q1[j][++tt1[j]] = i;
                while (hh2[j] <= tt2[j] && a[i][j] <= a[q2[j][tt2[j]]][j]) tt2[j]--;
                q2[j][++tt2[j]] = i;
            }
            // 将前 1 ~ x - 1列加入
            h1 = h2 = 0;
            t1 = t2 = -1;
            for (int j = 1; j < x; j++) {
                while (h1 <= t1 && a[q1[j][hh1[j]]][j] >= a[q1[qq1[t1]][hh1[qq1[t1]]]][qq1[t1]]) t1--;
                qq1[++t1] = j;
                while (h2 <= t2 && a[q2[j][hh2[j]]][j] <= a[q2[qq2[t2]][hh2[qq2[t2]]]][qq2[t2]]) t2--;
                qq2[++t2] = j;
            }

            for (int j = x; j <= m; j++) {
                // 将第x列加入
                while (h1 <= t1 && a[q1[j][hh1[j]]][j] >= a[q1[qq1[t1]][hh1[qq1[t1]]]][qq1[t1]]) t1--;
                qq1[++t1] = j;
                while (h2 <= t2 && a[q2[j][hh2[j]]][j] <= a[q2[qq2[t2]][hh2[qq2[t2]]]][qq2[t2]]) t2--;
                qq2[++t2] = j;
                res = Math.min(a[q1[qq1[h1]][hh1[qq1[h1]]]][qq1[h1]] - a[q2[qq2[h2]][hh2[qq2[h2]]]][qq2[h2]], res);
                // 将第 j - x + 1列弹出
                if (j - x + 1 == qq1[h1]) h1++;
                if (j - x + 1 == qq2[h2]) h2++;
            }
            // 将第i - x + 1 行弹出
            for (int j = 1; j <= m; j++) {
                if (i - x + 1 == q1[j][hh1[j]]) hh1[j]++;
                if (i - x + 1 == q2[j][hh2[j]]) hh2[j]++;
            }
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
